#include "cutenotes.h"
#include "ui_cutenotes.h"
#include <QDir>
#include <QFileInfo>
#include <QTextCodec>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextDocumentWriter>

CuteNotes::CuteNotes(QWidget *parent) : QMainWindow(parent),
ui(new Ui::CuteNotes)
{
    ui->setupUi(this);

    connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
    connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
    connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
    connect(ui->actionSaveAs, SIGNAL(triggered()), this, SLOT(saveAs()));

    const QStringList args = QCoreApplication::arguments();
    if (args.count() == 2)
    {
        QString initialFile = args.at(1);
        fileName = initialFile;
        loadFile();
    }

    if(fileName.isEmpty())
        CuteNotes::setWindowTitle("Untitled Document - CuteNotes");

}

CuteNotes::~CuteNotes()
{
    if(ui->textEdit->document()->isModified())
        maybeSave();
    delete ui;
}

bool CuteNotes::loadFile()
{
    if(!QFile::exists(fileName))
        return false;

    QFile file(fileName);
    if (!file.open(QFile::ReadOnly))
        return false;

    QByteArray data = file.readAll();
    QTextCodec *codec = Qt::codecForHtml(data);
    QString text = codec->toUnicode(data);

    if(Qt::mightBeRichText(text))
    {
        QMessageBox msgBox;
        int answer = msgBox.question(this, "Rich Text", "This file can be loaded as a rich text if you want.",
                                     QMessageBox::Yes, QMessageBox::No);

        if(answer == QMessageBox::Yes)
            ui->textEdit->setHtml(text);

        else
            ui->textEdit->setPlainText(text);
    }
    else
        ui->textEdit->setPlainText(text);

    return true;
}

void CuteNotes::openFile()
{
    QFileDialog dialog;
    fileName = dialog.getOpenFileName(this, tr("Open File"), QDir::homePath());
    if(!fileName.isEmpty())
        maybeSave();
        loadFile();
        setCurrentFileName();
}

void CuteNotes::newFile()
{
    if(maybeSave())
    {
        ui->textEdit->clear();
    }
        fileName.clear();
        setCurrentFileName();
}

bool CuteNotes::save()
{
    if(fileName.isEmpty())
        return saveAs();

    QTextDocumentWriter writer(fileName);
    bool ok = writer.write(ui->textEdit->document());

    if(!ok)
        return false;

    ui->textEdit->document()->setModified(false);
    return true;
}

bool CuteNotes::saveAs()
{
    QFileDialog dialog;
    fileName = dialog.getSaveFileName(this, tr("Save As"), QDir::homePath());
        if(fileName.isEmpty());
        return true;
    return save();
}

bool CuteNotes::maybeSave()
{
    if (!ui->textEdit->document()->isModified())
        return true;

    QMessageBox msgBox;
    int answer = msgBox.warning(this, tr("Document Modified"),
                                      tr("The document has been modified.\n Do you want to save your changes?"),
                                      QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
    if (answer == QMessageBox::Yes)
        return save();
    else if (answer == QMessageBox::No)
        return true;

    return false;
}

void CuteNotes::setCurrentFileName()
{
    if(fileName.isEmpty())
    {
       setWindowTitle(tr("Untitled Document - CuteNotes"));
    }

    else
    {
        QFileInfo fileInfo(fileName);
        setWindowTitle(tr("%1 - CuteNotes").arg(fileInfo.fileName()));
    }
}
